<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="省份" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省份"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['location:electricfence:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['location:electricfence:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['location:electricfence:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['location:electricfence:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="electricfenceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="行政区" align="center" prop="district" />
      <el-table-column label="自定义区域" align="center" prop="potarray" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['location:electricfence:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['location:electricfence:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改电子围栏对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body  >
      <div class="box">
        <div id='container' class="map"></div>
        <div style="float: left">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px" >
          <el-form-item label="省份" prop="province" >
            <el-select v-model="form.province" placeholder="请选择" @change="handleSelectCity">
              <el-option
                v-for="item in resultProvince"
                :key="item.adcode"
                :label="item.name"
                :value="item.name">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="城市"  prop="city">
            <el-select v-model="form.city" placeholder="请选择" @change="handleSelectDistric">
              <el-option
                v-for="item in resultCity"
                :key="item.adcode"
                :label="item.name"
                :value="item.name">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="行政区" prop="district">
            <el-select v-model="form.district" placeholder="请选择" >
              <el-option
                v-for="item in resultDistric"
                :key="item.adcode"
                :label="item.name"
                :value="item.name">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="自定义区域" prop="potarray" >
            <el-switch
              v-model="form.potarray"
              active-text="是"
              inactive-text="否"
              active-value="是"
              inactive-value="否">
            </el-switch>
          </el-form-item>

        </el-form>
          <div slot="footer" class="dialog-footer"  >
            <el-button   type="primary" @click="submitForm">确 定</el-button>
            <el-button  @click="cancel">取 消</el-button>
          </div>
        </div>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { listElectricfence, getElectricfence, delElectricfence, addElectricfence, updateElectricfence } from "@/api/location/electricfence";
import {initMap} from "@/api/location/AMap";

export default {
  name: "Electricfence",
  components: {
  },
  data() {
    return {

      //行政区
      distric:'',
      //行政区结果
      resultDistric:[],
      //城市
      city:'',
      //城市结果
      resultCity:[],
      //省结果
      resultProvince: [],
      //省份
      province: "",
      //地图
      map: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电子围栏表格数据
      electricfenceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        province: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询电子围栏列表 */
    getList() {
      this.loading = true;
      listElectricfence(this.queryParams).then(response => {
        this.electricfenceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        province: null,
        city: null,
        district: null,
        potarray: null,
        createrId: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加电子围栏";
      //this.toInitMap();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getElectricfence(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.toInitMap();
        this.title = "修改电子围栏";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            console.log(valid)
            updateElectricfence(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addElectricfence(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除电子围栏编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delElectricfence(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('location/electricfence/export', {
        ...this.queryParams
      }, `location_electricfence.xlsx`)
    },
    //初始化地图及其插件
    toInitMap(){
      initMap().then(map => {this.map = map;this.map.plugin('AMap.DistrictSearch');
        let districtSearch = new AMap.DistrictSearch({
          // 关键字对应的行政区级别，country表示国家
          level: 'country',
          //  显示下级行政区级数，1表示返回下一级行政区
          subdistrict: 1
        })
          let temp = this;
        // 搜索所有省/直辖市信息
        districtSearch.search('中国', function(status, result) {
          // 查询成功时，result即为对应的行政区信息
          temp.resultProvince = result.districtList[0].districtList
        })
        });
    },
    //城市选择框
    handleSelectCity(){
      let districtSearch = new AMap.DistrictSearch({
        // 关键字对应的行政区级别，country表示国家
        level: 'city',
        //  显示下级行政区级数，1表示返回下一级行政区
        subdistrict: 1
      })
      let that = this;
      let temp = this.form.province;
      // 搜索所有省/直辖市信息
      districtSearch.search(temp, function(status, result) {
        // 查询成功时，result即为对应的行政区信息
        that.resultCity = result.districtList[0].districtList;
        console.log(that.resultCity)
      })
    },
    handleSelectDistric(){
      let districtSearch = new AMap.DistrictSearch({
      // 关键字对应的行政区级别，country表示国家
      level: 'distric',
      //  显示下级行政区级数，1表示返回下一级行政区
      subdistrict: 1
    })
      let that = this;
      let temp = this.form.city;
      // 搜索所有省/直辖市信息
      districtSearch.search(temp, function(status, result) {
        // 查询成功时，result即为对应的行政区信息
        that.resultDistric = result.districtList[0].districtList;

      })

    },
  }
};
</script>
<style>
.map{
  width: 70%;
  height : calc(100vh - 120px);
  margin:0;
  padding:0;
  float: left;
}
.box{
  overflow: hidden;
}
.dialog-footer{
padding-left: 200px;
}

</style>
